﻿using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using FloatDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FloatDemo.Methods
{
    public class CanvasPointMethod
    {
        /// <summary>
        /// 返回屏幕坐标点
        /// </summary>
        /// <param name="targetPoint"></param>
        /// <param name="uView"></param>
        /// <returns></returns>
        public static CanvasPoint GetCanvasPoint(UIDocument uidoc, XYZ targetPoint)
        {
            UIView uView = uidoc.GetOpenUIViews()[0];
            var corners = uView.GetZoomCorners();
            double minX = corners.Min(x => x.X);
            double minY = corners.Min(x => x.Y);
            double maxX = corners.Max(x => x.X);
            double maxY = corners.Max(x => x.Y);
            CanvasPoint cp1 = new CanvasPoint();
            cp1.Top = 0 + (int)(FloatWindows.Instance.Height * (1 - (targetPoint.Y - minY) / (maxY - minY)));
            cp1.Left = 0 + (int)(FloatWindows.Instance.Width * (targetPoint.X - minX) / (maxX - minX));
            return cp1;
        }
        /// <summary>
        /// 窗体中的点转到Revit中
        /// </summary>
        /// <param name="point"></param>
        /// <param name="uView"></param>
        /// <returns></returns>
        public static XYZ GetRevitPoint(UIDocument uidoc, System.Windows.Point point)
        {
            UIView uView = uidoc.GetOpenUIViews()[0];
            var corners = uView.GetZoomCorners();
            double minX = corners.Min(x => x.X);
            double minY = corners.Min(x => x.Y);
            double maxX = corners.Max(x => x.X);
            double maxY = corners.Max(x => x.Y);
            double resultX = minX + (maxX - minX) * (point.X / FloatWindows.Instance.Width);
            double resultY = minY + (maxY - minY) * (1 - (point.Y / FloatWindows.Instance.Height));
            XYZ result = new XYZ(resultX,resultY,0);
            return result;
        }
    }
}
